//
//  SEGMyCollectionViewController.m
//  ExcellentHome
//
//  Created by Andy on 15/8/10.
//  Copyright (c) 2015年 陈区. All rights reserved.
//

#import "SEGMyCollectionViewController.h"
#import "MyCollectionTableViewCell.h"

@interface SEGMyCollectionViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) UITableView    *tableView; /**< 表格 */
@property (nonatomic ,strong) NSMutableArray *dataArray; /**< 数据 */
@property (nonatomic ,assign) NSInteger       page; /**< 分页 */
@property (nonatomic ,assign) BOOL           isRefresh; /**< 是否更新 */

@end

@implementation SEGMyCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的收藏";
    self.isRefresh = NO;
    self.dataArray = [[NSMutableArray alloc] init];
    [self showCustomNavigationBackButton];

    //表格列表
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorColor = kColor_L2;
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
    }
    [self setupRefresh];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)loadData {
    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"", @"pageSize",
                            @(self.page), @"pageNo",
                            kAccountUserID, @"userId", nil];
    
    WeakSelf;
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/praiseAttent/findCollectList.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        [weakSelf.tableView stopAnimatingWindmill];
        
        if (!isSuccessed) {
            [SEGAlertView showTmepMessageWithMessage:@"没有更多数据了！"];
            return;
        }
        
        NSDictionary* dict = result;
        NSMutableArray* tempArray = [[NSMutableArray alloc] init];
        for (NSDictionary* dic in [[dict valueForKey:@"data"] valueForKey:@"collectList"]) {
            [tempArray addObject:dic];
        }
        
        if (tempArray.count > 0) {
            weakSelf.page ++;
        }
        
        if (weakSelf.isRefresh) {
            weakSelf.isRefresh = NO;
            weakSelf.page = 1;
            weakSelf.dataArray = tempArray;
        } else {
            CGFloat totalSize = [[[dict valueForKey:@"data"] valueForKey:@"totalSize"] integerValue];
            if (weakSelf.dataArray.count < totalSize) {
                [weakSelf.dataArray addObjectsFromArray:tempArray];
            } else {
                [SEGAlertView showTmepMessageWithMessage:@"没有更多数据了！"];
            }
        }
        
        if ([weakSelf.dataArray count] == 0) {
            [[SEGUhomeMediator sharedInstance]seg_showCustomProgressImage:@{@"view":self.tableView}];
        } else {
            [[SEGUhomeMediator sharedInstance]seg_hideCustomProgressImage:@{@"view":self.tableView}];
        }
        
        [weakSelf.tableView reloadData];
    }];
}

- (void)setupRefresh {
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf headerRereshing];
    } footerRefreshBlock:^{
        [weakSelf footerRereshing];
    }];
    [self.tableView beginHeaderRefreshing];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing {
    self.isRefresh = YES;
    self.page = 1;
    [self loadData];
}

- (void)footerRereshing {
    self.isRefresh = NO;
    [self loadData];
}

#pragma mark -
#pragma mark table delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 1;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    static NSString* cellStr = @"cell";
    MyCollectionTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        cell = [[MyCollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }

    [cell onRequestData:[self.dataArray objectAtIndex:indexPath.item]];

    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary* dict = [self.dataArray objectAtIndex:indexPath.item];

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:[dict valueForKey:@"collectId"], @"pictorialId",[dict valueForKey:@"collectName"], @"objName", nil];
    UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initPGCDeteailViewController:params];
    [self.navigationController pushViewController:VC animated:YES];
}

@end
